<?php
  /**
   * @package foobar
   * @author Matt Reid
   * @copyright Copyright (c) 2009 Matt Reid
   * @license bar
   * @link http://site.com
   */

$g['root'] = $root;
$nroot = substr_replace($root,"",-1); //remove the trailing slash from the root path

print<<<HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Monarchs</title>

<link rel="stylesheet" type="text/css" href="$nroot/includes/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="$nroot/includes/extjs/resources/css/xtheme-slate.css" />

<script type="text/javascript" src="$nroot/includes/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="$nroot/includes/extjs/ext-all.js"></script>
<script type="text/javascript" src="$nroot/includes/extjs/Ext.ux.UUID.js"></script>

<script type="text/javascript">
  Ext.onReady(function() {
		Ext.QuickTips.init();
HEAD;

 $i = 1;
 print "\nvar dataList = [";
 $r=0;
 $u=count($data);
 foreach($data as $key => $value) {  
   foreach($value as $k => $v) {
     if($k == 'id') { $data_id=$v;}
     $$k = $v;
   }
   
   print "['$id','$command_objects_name','$command_objects_desc','$command_objects_command']\n";
   $r++;
   if($r<$u) { print ",\n";}
   else { 
     print "];\n";      
   }
   $lastID = ($id + 1);
 }
 
print<<<HEAD

  var command_objects_id_e = new Ext.form.TextField();  
 var command_objects_name_e = new Ext.form.TextField({maxLength: 255});
 var command_objects_desc_e = new Ext.form.TextField({maxLength: 1000});
 var command_objects_command_e = new Ext.form.TextField({maxLength: 1000});
 var nextID = $lastID;

 var dataStore = new Ext.data.SimpleStore({
   fields: [
	    {name: 'command_objects_id'},
	    {name: 'command_objects_name'},
	    {name: 'command_objects_desc'},
	    {name: 'command_objects_command'}
	    ]});

 var ds_model = Ext.data.Record.create([
   {name: 'command_objects_id'},
   {name: 'command_objects_name'},
   {name: 'command_objects_desc'},
   {name: 'command_objects_command'}
   ]);

 var dataGrid = new Ext.grid.EditorGridPanel ({
   renderTo: document.body,
       clickstoEdit: 2,
       xtype: 'grid',
       layout: 'fit',
       sm: new Ext.grid.RowSelectionModel({ singleSelect: true }),
       tbar: [
	      {
	      text: 'Add Command',
		  icon: '$nroot/includes/extjs/resources/images/slate/button/table_add.png',
		  cls: 'x-btn-text-icon',
		  scope: this,
		  handler: function() {
		  var conn = new Ext.data.Connection();
		  conn.request({
		    url: '$nroot/index.php/commands/submit/',
			params: {
		      action: 'insert'
			  },
			success: function(resp,opt) {
			var insert_id = Ext.util.JSON.decode(resp.responseText).insert_id;
			dataGrid.getStore().insert(
						   0,
						   new ds_model({
						     command_objects_id: nextID,
							 command_objects_name: 'NAME',
							 command_objects_desc: 'DESCRIPTION',
							 command_objects_command: 'COMMAND'
							 })
						   );
			nextID = nextID + 1;
			dataGrid.startEditing(0,0);
		      },
			failure: function(resp,opt) {
			Ext.Msg.alert('Error','Unable to add row');
		      }
		    });
		}
	      },
	      {
	      text: 'Remove Command',
		  icon: '$nroot/includes/extjs/resources/images/slate/button/table_delete.png',
		  cls: 'x-btn-text-icon',
		  handler: function() {
		  var sm = dataGrid.getSelectionModel();
		  var sel = sm.getSelected();
		  if (sm.hasSelection()) {
		    Ext.Msg.show({
		      title: 'Remove Row',
			  buttons: Ext.MessageBox.YESNO,
			  msg: 'Remove entry id: '+sel.data.command_objects_name+'?',
			  fn: function(btn){
			  if (btn == 'yes'){
			    var conn = new Ext.data.Connection();
			    conn.request({
			      url: '$nroot/index.php/commands/submit/',
				  params: {
				action: 'delete',
				    id: sel.data.command_objects_id
				    },
				  success: function(resp,opt) {
				  dataGrid.getStore().remove(sel);
				},
				  failure: function(resp,opt) {
				  Ext.Msg.alert('Error','Unable to delete row');
				}
			      });
			  }
			}
		      });
		  }
		}
	      }               
	      ],
       store: dataStore,
       columns: [                  
		 { id: 'command_objects_id', header: "ID", width: 30, sortable: true, dataIndex: 'command_objects_id'},
		 { id: 'command_objects_name', header: "Name", width: 60, sortable: true, dataIndex: 'command_objects_name', editor: command_objects_name_e },
		 { id: 'command_objects_desc', header: "Description", width: 250, sortable: false, dataIndex: 'command_objects_desc', editor: command_objects_desc_e },
		 { id: 'command_objects_command', header: "Command", width: 200, sortable: true, dataIndex: 'command_objects_command', editor: command_objects_command_e }
		 ],
       stripeRows: true,
       autoExpandColumn: 'command_objects_name',
       listeners: { 
     render: function(){ this.store.loadData(dataList); },
	 afteredit: function(e){
	 var conn = new Ext.data.Connection();
	 var dataID = e.record.data['command_objects_id'];
	 conn.request({
	   url: '$nroot/index.php/commands/submit/',
	       params: {
	     action: 'update',
		 id: dataID,
		 field: e.field,
		 value: e.value
		 },
	       success: function(resp,opt) {
	       e.record.commit();
	     },
	       failure: function(resp,opt) {
	       e.record.reject();
	     }
	   });
       }
      
     }})});
</script>
</head>
<body>
</body>
</html>
HEAD;
?>